home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Development
/
Source
/
Animation Routines
/
A - Basic Routines
next >
Wrap
Text File
|
1993-03-31
|
4KB
|
118 lines
{Andrew, author of Maelstrom, provided me with the following routines that help create color and noncolor ports and erase}
{them from memory. In order to use the routines provided in CellusoftAnimations, you must have this unit before the}
{Cellusoft animations unit but you do not need to understand how these routines work or how to call them.}
{CellusoftAnimations.p makes the task of creating ports and handling them a very easy task.}
{The following code may be used and distributed freely. Many thanks to the author of the hit game Maelstrom!}
unit BasicRoutines;
interface
function NewOffscreenPort (r: Rect): GrafPtr;
procedure DisposeOffscreenPort (offscreen: GrafPtr);
function NewOffScreenColorPort (bitrect: Rect): CGrafPtr; {This long function creates the PixMaps.}
procedure DisposeOffScreenColorPort (thePix: CGrafPtr);
implementation
function NewOffscreenPort (r: Rect): GrafPtr;
var
offscreen: GrafPtr;
savePort: GrafPtr;
baseAddr: Ptr;
rowBytes: Integer;
begin
GetPort(savePort);
offscreen := GrafPtr(NewPtr(sizeof(GrafPort)));
if offscreen <> nil then
begin
OpenPort(offscreen);
rowBytes := ((r.right - r.left + 15) div 16) * 2;
baseAddr := NewPtr(rowBytes * Longint((r.bottom - r.top)));
if baseAddr <> nil then
begin
offscreen^.portBits.rowBytes := rowBytes;
offscreen^.portBits.baseAddr := baseAddr;
offscreen^.portRect := r;
offscreen^.portBits.bounds := r;
RectRgn(offscreen^.clipRgn, r);
RectRgn(offscreen^.visRgn, r);
end
else
begin
ClosePort(offscreen);
DisposPtr(Ptr(offscreen));
offscreen := nil;
end
end;
SetPort(savePort);
NewOffscreenPort := offscreen;
end;
procedure DisposeOffscreenPort (offscreen: GrafPtr);
begin
DisposPtr(offscreen^.portBits.baseAddr);
ClosePort(offscreen);
DisposPtr(Ptr(offscreen));
end;
function NewOffScreenColorPort (bitrect: Rect): CGrafPtr; {This long function creates the PixMaps.}
var
prowbytes: longint;
width, height: longint;
bufferaddr: Ptr;
oldDev, deepdevicehdl: GDHandle;
pixdepth: longint;
buffersize: Longint;
aport: CGrafPtr;
saveport: GrafPtr;
prect: Rect;
begin
aport := nil;
prect := bitrect;
OffsetRect(prect, -prect.left, -prect.top);
oldDev := GetGDevice;
deepdevicehdl := GetMaxDevice(bitrect);
SetGDevice(deepdevicehdl);
pixdepth := deepdevicehdl^^.gdPMap^^.pixelSize;
width := bitrect.right - bitrect.left;
height := bitrect.bottom - bitrect.top;
prowbytes := Round((((pixdepth * width) + 15) / 16) * 2);
buffersize := height * prowbytes;
bufferaddr := NewPtr(buffersize);
prowbytes := prowbytes + $8000;
if (MemError = noErr) then
begin
GetPort(saveport);
aport := CGrafPtr((NewPtr(sizeof(CGrafPort))));
OpenCPort(aport);
aport^.portPixMap^^.baseAddr := bufferaddr;
aport^.portPixMap^^.rowBytes := prowbytes;
aport^.portPixMap^^.bounds := prect;
aport^.visRgn^^.rgnBBox := prect; {LC Call}
SetPort(GrafPtr(aport));
PortSize(width, height);
ClipRect(prect);
CopyRgn(aport^.clipRgn, aport^.visRgn);
SetPort(saveport);
end;
SetGDevice(oldDev);
NewOffScreenColorPort := aport;
end; { -- CreatePixMap }
procedure DisposeOffScreenColorPort (thePix: CGrafPtr); {This procedure destroys the offscreen pixmap passed to it }
begin
DisposPtr(thePix^.portPixMap^^.baseAddr);
CloseCPort(thePix);
DisposPtr(Ptr(thePix));
end; { -- KillPixMap }
end.